package flowcontrol.countdownlatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 场景：多个线程等待一个线程执行完成，在继续下一步
 */
public class TestCountDownLatch2 {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        ExecutorService service = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            final int k = i;
            service.submit(() -> {
                System.out.println("员工 " + k + "等待工作");
                try {
                    latch.await();
                    System.out.println("员工 " + k + "开始工作");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        service.shutdown();
        Thread.sleep(5000);
        // 老板开始下令工作
        System.out.println("所有员工等老板开始下令工作，老板说开始工作");
        latch.countDown();
    }
}
